﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ASCalpuff.FileLayers
{
    public class PG4:Group
    {
        /// <summary>
        /// 地图投影
        /// UTM：Universal Transverse Mercator
        /// TTM Tangential Transverse Mercator
        /// LCC：  Lambert Conformal Conic
        /// PS： Polar Stereographic
        /// EM  Equatorial Mercator
        /// LAZA：Lambert Azimuthal Equal Area
        /// </summary>
        private string m_PMAP = "UTM";
        /// <summary>
        /// 地图投影(UTM,TTM,LCC,PS,EM,LAZA)
        /// </summary>
        public string PMAP
        {
            get { return m_PMAP; }
            set { m_PMAP = value; }
        }
        /// <summary>
        /// 东西方向和南北方向的误差,  (Used only if PMAP= TTM, LCC, or LAZA)
        /// ! FEAST  = 0.000  !
        /// ! FNORTH = 0.000  !
        /// </summary>
        private double m_FEAST;
        /// <summary>
        /// 
        /// </summary>
        public double FEAST
        {
            get { return m_FEAST; }
            set { m_FEAST = value; }
        }
        /// <summary>
        /// 东西方向和南北方向的误差,  (Used only if PMAP= TTM, LCC, or LAZA)
        /// ! FEAST  = 0.000  !
        /// ! FNORTH = 0.000  !
        /// </summary>
        private double m_FNORTH;

        public double FNORTH
        {
            get { return m_FNORTH; }
            set { m_FNORTH = value; }
        }
        private int m_IUTMZN = 50;
        /// <summary>
        /// UTM区(1 to 60),(Used only if PMAP=UTM
        /// </summary>
        public int IUTMZN
        {
            get { return m_IUTMZN; }
            set { m_IUTMZN = value; }
        }
        /// <summary>
        /// UTM区的半球,(Used only if PMAP=UTM
        /// N：北半球
        /// S：南半球
        /// </summary>
        private string m_UTMHEM = "N";

        public string UTMHEM
        {
            get { return m_UTMHEM; }
            set { m_UTMHEM = value; }
        }
        /// <summary>
        /// 地图投影原点经纬度（decimal degrees）,(Used only if PMAP= TTM, LCC, PS, EM, or LAZA)
        /// ! RLAT0 = 10N  !
        /// ! RLON0 = 110E  !
        /// </summary>
        private string m_RLAT0 = "10N";

        public string RLAT0
        {
            get { return m_RLAT0; }
            set { m_RLAT0 = value; }
        }
        /// <summary>
        /// 地图投影原点经纬度（decimal degrees）,(Used only if PMAP= TTM, LCC, PS, EM, or LAZA)
        /// ! RLAT0 = 10N  !
        /// ! RLON0 = 110E  !
        /// </summary>
        private string m_RLON0 = "110E";

        public string RLON0
        {
            get { return m_RLON0; }
            set { m_RLON0 = value; }
        }
        /// <summary>
        /// 地图投影的标准纬线,(Used only if PMAP= LCC or PS)
        /// </summary>
        private string m_XLAT1 = "25N";

        public string XLAT1
        {
            get { return m_XLAT1; }
            set { m_XLAT1 = value; }
        }
        /// <summary>
        /// 地图投影的标准纬线,(Used only if PMAP= LCC or PS)
        /// </summary>
        private string m_XLAT2 = "40N";

        public string XLAT2
        {
            get { return m_XLAT2; }
            set { m_XLAT2 = value; }
        }
        /// <summary>
        /// 基准面代码
        /// </summary>
        private string m_DATUM = "WGS-84";

        public string DATUM
        {
            get { return m_DATUM; }
            set { m_DATUM = value; }
        }
        private int m_NX = 30;
        /// <summary>
        /// 气象网格中X轴方向上的网格单元数
        /// </summary>
        public int NX
        {
            get { return m_NX; }
            set { m_NX = value; }
        }
        private int m_NY = 30;
        /// <summary>
        /// 气象网格中Y轴方向上的网格单元数
        /// </summary>
        public int NY
        {
            get { return m_NY; }
            set { m_NY = value; }
        }
        private int m_NZ = 8;
        /// <summary>
        /// 气象网格中垂直层的数量
        /// </summary>
        public int NZ
        {
            get { return m_NZ; }
            set { m_NZ = value; }
        }
        private double m_DGRIDKM = 1.0;
        /// <summary>
        /// 气象网格的网格间距（km)
        /// </summary>
        public double DGRIDKM
        {
            get { return m_DGRIDKM; }
            set { m_DGRIDKM = value; }
        }
        private List<double> m_ArryZFACE;
        /// <summary>
        /// 气象网格的单元层高度（m)（必须输入NX+1的值）。注意点：第“i”单元层中心（网格点）高度等于((ZFACE(i+1)+(ZFACE(i))/2)
        /// </summary>
        public List<double> ArryZFACE
        {
            get { return m_ArryZFACE; }
            set { m_ArryZFACE = value; }
        }
        private double m_XORIGKM = 491.143;
        /// <summary>
        /// 气象网格中西南角的网格单元（１,１）的X坐标（KM）
        /// </summary>
        public double XORIGKM
        {
            get { return m_XORIGKM; }
            set { m_XORIGKM = value; }
        }
        private double m_YORIGKM = 4050.045;
        /// <summary>
        /// 气象网格中西南角的网格单元（１,１）的Y坐标（KM）
        /// </summary>
        public double YORIGKM
        {
            get { return m_YORIGKM; }
            set { m_YORIGKM = value; }
        }
        private int m_IBCOMP = 1;
        /// <summary>
        /// CALPUFF计算网格的左下角的X指数（１<=IBCOMP<=NX )，可以小于CALMET的气象网格
        /// </summary>
        public int IBCOMP
        {
            get { return m_IBCOMP; }
            set { m_IBCOMP = value; }
        }
        private int m_JBCOMP = 1;
        /// <summary>
        /// 计算网格的左下角的Y指数（１<=JBCOMP<=NY )
        /// </summary>
        public int JBCOMP
        {
            get { return m_JBCOMP; }
            set { m_JBCOMP = value; }
        }
        private int m_IECOMP = 30;
        /// <summary>
        /// 计算网格的左下角的X指数（IBCOMP<=IECOMP<=NX )
        /// </summary>
        public int IECOMP
        {
            get { return m_IECOMP; }
            set { m_IECOMP = value; }
        }
        private int m_JECOMP = 30;
        /// <summary>
        /// 计算网格的左下角的Y指数（JBCOMP<=JECOMP<=NY )
        /// </summary>
        public int JECOMP
        {
            get { return m_JECOMP; }
            set { m_JECOMP = value; }
        }
        private string m_LSAMP = "T";
        /// <summary>
        /// 表明是否使用网格化受体（如：采样网格）的标记（T=是，F＝否）
        /// </summary>
        public string LSAMP
        {
            get { return m_LSAMP; }
            set { m_LSAMP = value; }
        }
        private int m_IBSAMP = 1;
        /// <summary>
        /// 采样网格的左下角的X网格数（IBCOMP<=IBSAMP<=IECOMP)
        /// </summary>
        public int IBSAMP
        {
            get { return m_IBSAMP; }
            set { m_IBSAMP = value; }
        }
        private int m_JBSAMP = 1;
        /// <summary>
        /// 采样网格的左下角的Y网格数（JBCOM<=JBSAMP<=JECOMP)
        /// </summary>
        public int JBSAMP
        {
            get { return m_JBSAMP; }
            set { m_JBSAMP = value; }
        }
        private int m_IESAMP = 30;
        /// <summary>
        ///  采样网格的左下角的X网格数（IBCOMP<=IBSAMP<=IECOMP)
        /// </summary>
        public int IESAMP
        {
            get { return m_IESAMP; }
            set { m_IESAMP = value; }
        }
        private int m_JESAMP = 30;
        /// <summary>
        ///  采样网格的左下角的Y网格数（JBCOM<=JBSAMP<=JECOMP)
        /// </summary>
        public int JESAMP
        {
            get { return m_JESAMP; }
            set { m_JESAMP = value; }
        }
        private int m_MESHDN = 1;
        /// <summary>
        /// 采样网格的嵌套因子（MESHDN>1).采样网格的网格间隔是DGRIDKM/MESHDN。采样网格点的数据是NXSAM*NYSAM,其中，NXSAM=MESHDN*(IESAMP-IBSAMP)+1,NYSAM=MESHDN*(JESAMP-JBSAMP)+1
        /// </summary>
        public int MESHDN
        {
            get { return m_MESHDN; }
            set { m_MESHDN = value; }
        }

        public override void ReadFromGroup(Dictionary<string, string> dic)
        {
            foreach (KeyValuePair<string, string> m_dic in dic)
            {

                if (m_dic.Key == "PMAP")
                {
                    this.PMAP = m_dic.Value;
                }
                if (m_dic.Key == "FEAST")
                {
                    this.FEAST = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "FNORTH")
                {
                    this.FNORTH = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "IUTMZN")
                {
                    this.IUTMZN = Convert.ToInt32(m_dic.Value);
                }

                if (m_dic.Key == "UTMHEM")
                {
                    this.UTMHEM = m_dic.Value;
                }
                if (m_dic.Key == "RLAT0")
                {
                    this.RLAT0 = m_dic.Value;
                }
                if (m_dic.Key == "RLON0")
                {
                    this.RLON0 = m_dic.Value;
                }
                if (m_dic.Key == "XLAT1")
                {
                    this.XLAT1 = m_dic.Value;
                }

                if (m_dic.Key == "XLAT2")
                {
                    this.XLAT2 = m_dic.Value;
                }
                if (m_dic.Key == "DATUM")
                {
                    this.DATUM = m_dic.Value;
                }
                if (m_dic.Key == "NX")
                {
                    this.NX = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "NY")
                {
                    this.NY = Convert.ToInt32(m_dic.Value);
                }

                if (m_dic.Key == "NZ")
                {
                    this.NZ = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "DGRIDKM")
                {
                    this.DGRIDKM = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "ZFACE")
                {
                    this.ArryZFACE = StrWriter.ReturnDouList(m_dic.Value);
                }
                if (m_dic.Key == "XORIGKM")
                {
                    this.XORIGKM = Convert.ToDouble(m_dic.Value);
                }

                if (m_dic.Key == "YORIGKM")
                {
                    this.YORIGKM = Convert.ToDouble(m_dic.Value);
                }
                if (m_dic.Key == "IBCOMP")
                {
                    this.IBCOMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "JBCOMP")
                {
                    this.JBCOMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "IECOMP")
                {
                    this.IECOMP = Convert.ToInt32(m_dic.Value);
                }

                if (m_dic.Key == "JECOMP")
                {
                    this.JECOMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "LSAMP")
                {
                    //if (m_dic.Key == "T")
                    //{

                    //}
                    //else
                    //{ 
                    //}
                    this.LSAMP = m_dic.Value;
                }
                if (m_dic.Key == "IBSAMP")
                {
                    this.IBSAMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "JBSAMP")
                {
                    this.JBSAMP = Convert.ToInt32(m_dic.Value);
                }

                if (m_dic.Key == "IESAMP")
                {
                    this.IESAMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "JESAMP")
                {
                    this.JESAMP = Convert.ToInt32(m_dic.Value);
                }
                if (m_dic.Key == "MESHDN")
                {
                    this.MESHDN = Convert.ToInt32(m_dic.Value);
                }

            }
        }
        public override string WriteToGroup()
        {
            string ResultStr = "", TempStr;

            TempStr = StrWriter.GetCLine();
            TempStr += "INPUT GROUP: 4 -- Map Projection and Grid control parameters" + "\r\n";
            ResultStr += TempStr;

            TempStr = StrWriter.GetCLine();
            ResultStr += TempStr;


            if (PMAP != "")
                TempStr = StrWriter.GetKeyValStr("PMAP", PMAP);
            else
                TempStr = StrWriter.GetNullKeyValStr("PMAP");
            ResultStr += TempStr;

            TempStr = "! FEAST = " + FEAST.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! FNORTH = " + FNORTH.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! IUTMZN = " + IUTMZN.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! UTMHEM = " + UTMHEM + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! RLAT0 = " + RLAT0 + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! RLON0 = " + RLON0 + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! XLAT1 = " + XLAT1 + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! XLAT2 = " + XLAT2 + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! DATUM = " + DATUM + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! NX = " + NX.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! NY = " + NY.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! NZ = " + NZ.ToString() + " !" + "\r\n";
            ResultStr += TempStr;



            TempStr = "! DGRIDKM = " + DGRIDKM.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! ZFACE =   ";
            if (ArryZFACE.Count > 0)
            {
                for (int i = 0; i < ArryZFACE.Count; i++)
                {
                    if (i == ArryZFACE.Count - 1)
                    {
                        TempStr += ArryZFACE[i];

                    }
                    else
                    {
                        TempStr += ArryZFACE[i] + ",   ";
                    }

                }
                TempStr += "  !  " + "\r\n";
                ResultStr += TempStr;
            }


            TempStr = "! XORIGKM = " + XORIGKM.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! YORIGKM = " + YORIGKM.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! IBCOMP = " + IBCOMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! JBCOMP = " + JBCOMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! IECOMP = " + IECOMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! JECOMP = " + JECOMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! LSAMP = " + LSAMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! IBSAMP = " + IBSAMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! JBSAMP = " + JBSAMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! IESAMP = " + IESAMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;


            TempStr = "! JESAMP = " + JESAMP.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            TempStr = "! MESHDN = " + MESHDN.ToString() + " !" + "\r\n";
            ResultStr += TempStr;

            ResultStr += "!END!" + "\r\n";

            return ResultStr;
        }
        /// <summary>
        /// 得到字符串中的数值
        /// </summary>
        /// <param name="Val"></param>
        /// <returns></returns>
        public double GetValue(string Val)
        {
            if (Val != null)
            {
                if (Val.Length > 1)
                {
                    double A = Convert.ToDouble(Val.Remove(Val.Length - 1, 1));
                    return A;
                }
                else
                {
                    return 0;
                }
            }
            else
            {
                return 0;
            }

        }
        /// <summary>
        /// 得到字符串中最后一个字符
        /// </summary>
        /// <param name="Str"></param>
        /// <returns></returns>
        public string GetString(string Str)
        {
            if (Str != null)
            {
                return Str.Remove(0, Str.Length - 1);
            }
            else
            {
                return "";
            }

        }

    }
}
